package com.ytktt.study.week7.task2.repository;

import com.ytktt.study.week7.task2.entity.OrderInfoEntity;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.List;

/**
 * 订单信息仓库
 *
 * @author ytk
 */
@Slf4j
public class OrderInfoRepository {

    private OrderInfoRepository() {}

    private static class StudyRepositoryHolder {
        private static final OrderInfoRepository HOLDER = new OrderInfoRepository();
    }

    public static final OrderInfoRepository getInstance() {
        return StudyRepositoryHolder.HOLDER;
    }

    public void loopBatch(List<OrderInfoEntity> list, Connection connection, String sql) throws SQLException {
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            for (OrderInfoEntity entity : list) {
                preparedStatement.setString(1, entity.getId());
                preparedStatement.setInt(2, entity.getUserId());
                preparedStatement.setInt(3, entity.getItemId());
                preparedStatement.setBigDecimal(4, entity.getItemPrice());
                preparedStatement.setInt(5, entity.getAmount());
                preparedStatement.setBigDecimal(6, entity.getOrderPrice());
                preparedStatement.setByte(7, entity.getDelFlag());
                preparedStatement.setByte(8, entity.getStatus());
                preparedStatement.setTimestamp(9, entity.getCreateTime());
                preparedStatement.setTimestamp(10, entity.getUpdateTime());
                preparedStatement.executeUpdate();
            }
        }
    }

    /**
     * 批量插入 也可以使用INSERT INTO tb_study(name) VALUES (?, ?, ?...)的方式
     * @param list
     * @param connection
     * @throws SQLException
     */
    public void saveBatch(List<OrderInfoEntity> list, Connection connection, String sql) throws SQLException {
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            for (OrderInfoEntity entity : list) {
                preparedStatement.setString(1, entity.getId());
                preparedStatement.setInt(2, entity.getUserId());
                preparedStatement.setInt(3, entity.getItemId());
                preparedStatement.setBigDecimal(4, entity.getItemPrice());
                preparedStatement.setInt(5, entity.getAmount());
                preparedStatement.setBigDecimal(6, entity.getOrderPrice());
                preparedStatement.setByte(7, entity.getDelFlag());
                preparedStatement.setByte(8, entity.getStatus());
                preparedStatement.setTimestamp(9, entity.getCreateTime());
                preparedStatement.setTimestamp(10, entity.getUpdateTime());
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        }
    }

    public void multiValueSave(List<OrderInfoEntity> list, Connection connection, String sql) throws SQLException {
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            int index = 0;
            for (OrderInfoEntity entity : list) {
                preparedStatement.setString(++index, entity.getId());
                preparedStatement.setInt(++index, entity.getUserId());
                preparedStatement.setInt(++index, entity.getItemId());
                preparedStatement.setBigDecimal(++index, entity.getItemPrice());
                preparedStatement.setInt(++index, entity.getAmount());
                preparedStatement.setBigDecimal(++index, entity.getOrderPrice());
                preparedStatement.setByte(++index, entity.getDelFlag());
                preparedStatement.setByte(++index, entity.getStatus());
                preparedStatement.setTimestamp(++index, entity.getCreateTime());
                preparedStatement.setTimestamp(++index, entity.getUpdateTime());
            }
            preparedStatement.executeUpdate();
        }
    }

    public void truncate(Connection connection) throws SQLException {
        String sql = "TRUNCATE tb_order_info";
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.execute();
        }
    }
}
